﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using System.Collections;
using System.Collections.ObjectModel;
using System.Net;
using System.Net.Mail;
using System.Reflection;
using System.ComponentModel; 
using System.Collections.Specialized;
using DataLayer.dbml;





/// <summary>
/// Descripción breve de cGlobal
/// </summary>
public class cGlobal : System.Web.UI.Page
{
    public cGlobal()
    {
        //
        // TODO: Agregar aquí la lógica del constructor
        //
    }

    #region declarations

    public string MailFrom = ""; //ConfigurationManager.AppSettings["MailFrom"].ToString();
    public string MailName = ""; //ConfigurationManager.AppSettings["MailName"].ToString();

    public string urlPortal = ConfigurationManager.AppSettings["urlPortal"];
    public string formatDate = "dd/MM/yyyy";
    //public string SFUser = ConfigurationManager.AppSettings["SFUser"].ToString();
    //public string SFPwd = ConfigurationManager.AppSettings["SFPwd"].ToString();
    public static string WSUser = ConfigurationManager.AppSettings["WSUser"].ToString();
    public static string WSPwd = ConfigurationManager.AppSettings["WSPwd"].ToString();

    //public SForceOTService.OrdenDeTrabajoWebServiceService OTService = new SForceOTService.OrdenDeTrabajoWebServiceService();
    //public SForceOPService.wsCreacionoportunidaSFDC_clsService OPService = new SForceOPService.wsCreacionoportunidaSFDC_clsService();
    //public SForceCase.wsCreacionCasoSFDC_clsService CService = new SForceCase.wsCreacionCasoSFDC_clsService();
    //public SForceCaseComment.wsCreacionComentarioCasoSFDC_clsService CCService = new SForceCaseComment.wsCreacionComentarioCasoSFDC_clsService();

    #endregion 

    #region utilities



    /// <summary>
    /// Carga los DropDownList
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ddl"></param>
    /// <param name="list"></param>
    /// <param name="TextField"></param>
    /// <param name="ValueField"></param>
    /// <param name="addItem"></param>
    /// <param name="valueItem"></param>
    /// <returns></returns>
    public Boolean dropDownFill<T>(DropDownList ddl, List<T> list, string TextField, string ValueField, string addItem, string valueItem)
    {
        ddl.DataSource = list;
        ddl.DataValueField = ValueField;
        ddl.DataTextField = TextField;
        ddl.DataBind();
        if (addItem != null)
        {
            ddl.Items.Insert(0, addItem);
            ddl.Items[0].Value = valueItem;
        }
        ddl.SelectedIndex = 0;
        return true;
    }

    /// <summary>
    /// Carga un dropdownlist con el datatable que recibe
    /// </summary>
    /// <param name="ddl"></param>
    /// <param name="dtt"></param>
    /// <param name="TextField"></param>
    /// <param name="ValueField"></param>
    /// <param name="addItem"></param>
    /// <param name="valueItem"></param>
    /// <returns></returns>
    public Boolean dropDownFill(DropDownList ddl, DataTable dtt, string TextField, string ValueField, string addItem, string valueItem)
    {
        ddl.DataSource = dtt;
        ddl.DataValueField = ValueField;
        ddl.DataTextField = TextField;
        ddl.DataBind();
        if (addItem != null)
        {
            ddl.Items.Insert(0, addItem);
            ddl.Items[0].Value = valueItem;
        }
        ddl.SelectedIndex = 0;
        return true;    
    }

    public static string GetMd5Hash(string input)
    {
        // Create a new instance of the MD5CryptoServiceProvider object.
        System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create();
        // Convert the input string to a byte array and compute the hash.
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
        // Create a new Stringbuilder to collect the bytes and create a string.
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sb.Append(data[i].ToString("x2"));
        }
        return sb.ToString();
    }




    #endregion

    #region js
    /// <summary>
    /// Mensaje de Alerta
    /// </summary>
    /// <param name="strMensaje"></param>
    /// <param name="Pagina"></param>
    public void jsAlert(string strMensaje, Page Pagina)
    {
        string strScript = string.Empty;
        strScript = "<script language='javascript'>";
        strScript += " alert('" + strMensaje + "');";
        strScript += "</script>";
        Pagina.ClientScript.RegisterStartupScript(typeof(String),"MsgBox", strScript);
    }

    /// <summary>
    /// Cierra el shadow y refrescha la iframe
    /// </summary>
    /// <param name="Pagina"></param>
    /// <param name="nameIframe"></param>
    /// <param name="pageRefresh"></param>
    public void jsCloseShadowBox(Page Pagina, string nameIframe, string pageRefresh)
    {
        string strScript = string.Empty;
        strScript = "<script language='javascript'>";

        if(nameIframe != string.Empty)
            strScript += "parent." + nameIframe + ".location = '" + pageRefresh + "';";

        strScript += "parent.Shadowbox.close();";
        strScript += "</script>";
        Pagina.ClientScript.RegisterStartupScript(typeof(String), "Shadow", strScript);
    }
    #endregion


    #region SalesForce

    //public SForcePartner.LoginResult SFAuthentication()
    //{
    //    SForcePartner.LoginResult lr = new SForcePartner.LoginResult();
    //    SForcePartner.SforceService ss = new SForcePartner.SforceService();
        
    //    lr = ss.login(SFUser, SFPwd);

    //    //SForceService.LoginResult lr = new SForceService.LoginResult();        
    //    //SForceService.SforceService ss = new SForceService.SforceService();
        
    //    OTService.SessionHeaderValue = new SForceOTService.SessionHeader();
    //    OTService.SessionHeaderValue.sessionId = lr.sessionId;

    //    OPService.SessionHeaderValue = new SForceOPService.SessionHeader();
    //    OPService.SessionHeaderValue.sessionId = lr.sessionId;
        
    //    CService.SessionHeaderValue = new SForceCase.SessionHeader();
    //    CService.SessionHeaderValue.sessionId = lr.sessionId;

    //    CCService.SessionHeaderValue = new SForceCaseComment.SessionHeader();
    //    CCService.SessionHeaderValue.sessionId = lr.sessionId;

    //    return lr;
    //}
    
   
    #endregion






}